package com.wedo.mall.activity.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.wedo.mall.R;
import com.wedo.mall.WApp;
import com.wedo.mall.activity.agent.AgentDetailActivity;
import com.wedo.mall.activity.login.WLogin;
import com.wedo.mall.entity.Agent;
import com.wedo.mall.entity.Goods;
import com.wedo.mall.util.WUrl;
import com.wedo.mall.widget.EWebView;
import com.wedo.mall.widget.GoodsBanner;
import com.wedo.mall.widget.StarView;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

public class PointGoodsDetailActivity extends EActivity {

    public static final String EXTRA_GOODS_ID = "goods_id";

    @BindView(R.id.cb_banner) ConvenientBanner cb_banner;
    @BindView(R.id.tv_goods) TextView tv_goods;
    @BindView(R.id.iv_collection) ImageView iv_collection;
    @BindView(R.id.tv_intro) TextView tv_intro;
    @BindView(R.id.tv_price) TextView tv_price;
    @BindView(R.id.tv_brow) TextView tv_brow;
    @BindView(R.id.tv_sale) TextView tv_sale;
    @BindView(R.id.btn_auth) Button btn_auth;
    @BindView(R.id.ll_web) LinearLayout ll_web;
    private EWebView eWebView;

    /**店铺信息*/
    @BindView(R.id.iv_img) ImageView iv_img;
    @BindView(R.id.tv_name) TextView tv_agent_name;
    @BindView(R.id.tv_distance) TextView tv_distance;
    @BindView(R.id.sv_star) StarView sv_star;
    @BindView(R.id.v_divider) View v_divider;

    @BindView(R.id.rl_root) View rl_root;


    private Goods goods;
    private EResponse response;
    private Agent agent;
    private String collect_id;

    private String goods_id;

    public static void toDetail(EActivity activity,String goods_id){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_GOODS_ID,goods_id);
        activity.toActivity(PointGoodsDetailActivity.class,bundle);
    }

    @Override
    protected int layout() {
        return R.layout.activity_point_goods_detail;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        eWebView = new EWebView(ll_web);

        goods_id = getIntent().getStringExtra(EXTRA_GOODS_ID);
        Map<String,String> params = WApp.lngLat();
        params.put(EXTRA_GOODS_ID,goods_id);
        loading();
        post(EApi.GOODS_DETAIL, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                detail(response);
                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        isCorE();
    }

    @Override
    protected void onPause() {
        super.onPause();
        isCorE();
    }

    private void detail(EResponse response) {
        this.response = response;

        /**商品banner*/
        List<String> images = response.list("goodsImages",String.class);
        cb_banner.setPages(new CBViewHolderCreator() {
            @Override
            public Object createHolder() {
                return new GoodsBanner();
            }
        },images);

        /**商品信息*/
        goods = response.object("goodsInfo",Goods.class);
        //goodsId = goods.id;//默认购买该商品
        tv_goods.setText(goods.goods_name);
        tv_intro.setText(goods.introduction);
        tv_price.setText(String.format(getString(R.string.user_integral),goods.goods_price));
        tv_brow.setText(String.format(getString(R.string.user_browse),goods.browsing_number));
        tv_sale.setText(String.format(getString(R.string.user_selling),goods.sale_number));
        btn_auth.setVisibility(TextUtils.equals("1",goods.is_auth)? View.VISIBLE:View.GONE);

        String is_collect = response.string("is_collect","0");
        collect_id = response.string("collect_id");
        iv_collection.setSelected(TextUtils.equals("1",is_collect));

        String goodsDesc = response.string("goodsDesc");
        eWebView.data(goodsDesc);

        /**店铺信息*/
        /**http://Merchant.wedo.my//Static/Uploads/Goods/2017-11-07/5a0125ec500af.jpg*/
        agent = response.object("agentInfo",Agent.class);
        if(agent !=null && !TextUtils.isEmpty(agent.id) && !TextUtils.isEmpty(agent.agent_name)) {
            rl_root.setVisibility(View.VISIBLE);
            agent.id =goods.agent_id;//bug fix
            tv_agent_name.setText(agent.agent_name);
            tv_distance.setText(String.format("%skm",agent.distance));
            sv_star.star(agent.star);
            v_divider.setVisibility(View.GONE);
            Glide.with(this).load(WUrl.url(agent.logo)).error(R.drawable.place_holder_agent).placeholder(R.drawable.place_holder_agent).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_img);
        }else {
            rl_root.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.iv_backward)
    protected void onBackClick(){
        defaultFinish();
    }

    @OnClick(R.id.iv_collection)
    protected void onCollectionClick(){
        if (iv_collection.isSelected()){
            delCollect();
        }else {
            toCollect();
        }
    }

    @OnClick(R.id.btn_buy_now)
    protected void onClick(){
        if(!WLogin.isBinding(this)) {
            return;
        }
        ExchangeActivity.toDetail(PointGoodsDetailActivity.this,goods_id,"0");
    }

    @OnClick(R.id.rl_root)
    protected void onAgentClick(){
        AgentDetailActivity.toDetail(this,agent.id);
    }

    private void toCollect(){
        if(!WLogin.isLogged(this)) {
            return;
        }

        String id = goods.id;
        String type = "0";

        loading();
        Observable<EResponse> observable = api().collect(id,type);
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                collect_id = response.string("collect_id");
                iv_collection.setSelected(true);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    private void delCollect(){
        loading();
        Observable<EResponse> observable = api().delCollect(collect_id);
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                iv_collection.setSelected(false);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    @Override
    protected void onDestroy() {
        eWebView.onDestroy();
        super.onDestroy();
    }


}
